package com.glepoch.springbootsecuritymybatis.compoment.easyexcel;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.glepoch.springbootsecuritymybatis.compoment.easyexcel.entity.StudentEntity;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExportDemo {


    public static void main(String[] args) throws FileNotFoundException {
        List<StudentEntity> students =new ArrayList<>();
        //学生
        StudentEntity studentEntity0=new StudentEntity();
        studentEntity0.setId("0");
        studentEntity0.setName("张0");
        studentEntity0.setSex(0);
        studentEntity0.setRegDate(new Date());

        StudentEntity studentEntity1=new StudentEntity();
        studentEntity1.setId("1");
        studentEntity1.setName("张1");
        studentEntity1.setSex(1);
        studentEntity1.setRegDate(new Date());

        StudentEntity studentEntity2=new StudentEntity();
        studentEntity2.setId("2");
        studentEntity2.setName("张2");
        studentEntity2.setSex(0);
        studentEntity2.setRegDate(new Date());
        students.add(studentEntity0);
        students.add(studentEntity1);
        students.add(studentEntity2);

        FileOutputStream fos=new FileOutputStream("D:/1.xls");//可以传入response 获取的流实现下载
        //多sheel导出示例
        ExcelWriter excelWriter = EasyExcel.write(fos).build();
        WriteSheet sheet1 = EasyExcel.writerSheet(0, "表0").head(StudentEntity.class).build();
        excelWriter.write(students,sheet1);
        WriteSheet sheet2 = EasyExcel.writerSheet(1, "表1").head(StudentEntity.class).build();
        excelWriter.write(students,sheet2);
        excelWriter.finish();

        //简单sheel导出示例
        //EasyExcel.write(fos, StudentEntity.class).sheet("学生表").doWrite(students);
    }
}


